import {
  TonSku,
  TonSkuGateway,
  searchTonSkuDto,
  searchTonSkuRes,
  getTonSkuByIdRes,
  getTonSkuByIdDto,
  getTonSkuByOrderIdDto,
  TonSkuReadInterfaceWithGrant,
  getTonSkuByOrderIdRes,
} from "qqlx-core";

import { ViewBase, ViewBaseAction } from "../view-base";
import { TonSkuSchema } from "@/river/ton-sku";

export class ViewTonSku extends ViewBase<TonSku> implements ViewBaseAction<TonSku>, TonSkuReadInterfaceWithGrant<null> {
  private static instance: ViewTonSku;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewTonSku();
    }

    if (!this.instance) this.instance = new ViewTonSku();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): TonSku {
    return new TonSkuSchema();
  }

  async getByOrderId(input: Omit<getTonSkuByOrderIdDto, "page" | "conditionList">) {
    this.pending();
    const base = { page: this.data.entityListPage, conditionList: this.data.conditionList };
    const dto = { ...base, ...input };

    const res = await this.$request.getByPut<getTonSkuByOrderIdDto, getTonSkuByOrderIdRes>(TonSkuGateway.path, dto);
    this.data.entityListTotal = res.total;
    return res;
  }

  async getById(dto: getTonSkuByIdDto) {
    this.pending();
    const path = `${TonSkuGateway.path}/id`;
    return this.$request.getByPut<getTonSkuByIdDto, getTonSkuByIdRes>(path, dto);
  }

  async search(input: Omit<searchTonSkuDto, "page" | "conditionList">) {
    this.pending();
    const dto = { ...input, page: this.data.entityListPage, conditionList: this.data.conditionList };

    const path = `${TonSkuGateway.path}/search`;
    const { list, total } = await this.$request.getByPut<searchTonSkuDto, searchTonSkuRes>(path, dto);
    this.data.entityListTotal = total;
    return { list, total };
  }
}
